#ifndef DATABASE_H_INCLUDED
#define DATABASE_H_INCLUDED

#include <winsock2.h>
#include <mysql.h>
using namespace std;
class DATABASE{
private:
    MYSQL mysql;
    MYSQL_RES* res;
    MYSQL_ROW row;
public:
    DATABASE()
    {
        mysql_init(&mysql);
        res = NULL;

        if (!mysql_real_connect(&mysql, "127.0.0.1", "testuser", "testuser123", "testdb", 3306, 0, 0))
        {
            cerr << "mysql_real_connect: "  << mysql_error(&mysql)  << endl ;
            //return 0;
        }

        if (mysql_set_character_set(&mysql, "GBK") != 0)
        {
            cout << "gbk " << endl;
            //cin.get();
            cerr << "mysql_set_character_set: "  << mysql_error(&mysql)  << endl ;
            //return 0;
        }

        if (mysql_select_db(&mysql, "testdb") != 0) {
            cerr << "mysql_select_db: " << mysql_error(&mysql) << endl;
            mysql_close(&mysql);
            //return 0;
        }
    }
    ~DATABASE()
    {
        mysql_free_result(res);
        mysql_close(&mysql);
    }
    bool AddMember(char* sql)
    {
        //sql = "insert into iyoga(NAME, ADDRESS, JOB) values ('dd', 'b', 'c')";
        if (mysql_query(&mysql, sql) != 0) {
            cerr << "mysql_query: " << mysql_error(&mysql) << endl;
            mysql_close(&mysql);
            return true;
        }
    }
    //bool AddCard(char*, char*);
    bool ShowTable()
    {
        char* sql = "select * from iyoga";
        if (mysql_real_query(&mysql, sql, strlen(sql)) != 0)
        {
            cerr << "mysql_real_query: "  << mysql_error(&mysql)  << endl ;
            mysql_close(&mysql);
            return 0;
        }

        res = mysql_store_result(&mysql);
        if (NULL == res)
        {
            cerr << "mysql_store_result: "  << mysql_error(&mysql)  << endl ;
            mysql_close(&mysql);
            return 0;
        }

        int j=mysql_num_fields(res);

        while (row = mysql_fetch_row(res))
        {
            for (int i = 0; i < j; i++) {
                if (row[i]) {
                    cout << row[i] << '\t';
                }
            }
            cout << endl;
        }
    }
};

#endif // DATABASE_H_INCLUDED
